/* ========================================================================== */
/*                                                                            */
/*   Toggle.c                                                                 */
/*   (c) 2008 Ruud Nabben <r.nabben@gmail.com>                                */
/*                                                                            */
/*   Simple program to test IO ports and interrupt mechanism                  */
/*                                                                            */
/* ========================================================================== */

/* 
 * Compile with:
 *    zcc +embedded ns16450.o toggle.c -otoggle.bin -lim2 -lembedded_clib2
 *    
 * Convert to VHDL with:
 *   hex2rom.exe -b toggle.bin toggle 16l8z > toggle.vhd
 *  Make sure ram size and address are defined correctly in embedded_crt0.asm
 *  
 * Use makelib.bat to compile embedded_clib2 and ns16450.o
 * 
 *zcc +embedded ns16450.o toggle.c -o toggle.bin -lim2  -I. -lembedded_clib2     
 */  
 
 
 //usage:
 //1:  zcc +embedded ns16450.o bootloader.c -obootloader.bin -lim2 -lembedded_clib2
 //2:  hex2rom.exe -b bootloader.bin bootloader 16l8a > bootloader.vhd
 

#include <stdio.h>
#include <stdlib.h>
#include <im2.h>
#include "ns16450.h"
#include "keymap.h"

#define INT_DISABLE()   asm("di")
#define INT_ENABLE()    asm("ei")

#define IOPORT1_BASE		0x0010
#define IOPORT0_BASE		0x0020
#define KEYBOARD_BASE		0x0030

void main()
{
  char *ram = (char *) 0x0000;
  
  INT_DISABLE();
      
  /* convert 24 MHz to 16*baudrate
         9600 : 0x9c
       115200 : 0x0d
  */
  init_uart(0x00, 0x0d); 

  printf("Z80 Bootloader>\n\r");
 
  for (;;)
  {
    char UARTsr = 0;
    
    
    UARTsr = inp(UART_BASE + LSR);
    if (UARTsr & 0x01)
    {
      char ch;
      ch = inp(UART_BASE + RBR);
      /* assume uart can send faster than we can fill buffer,
      so no checking of lsr */
      *ram = ch;
      ram++;
      outp(UART_BASE + THR, '.');
    }
  }
}


